﻿using Microsoft.Extensions.Logging;
using ORS.OrderRobot.IOC;
using ORS.OrderRobot.ServiceContracts;
using ORS.OrderRobot.ServiceContracts.Dtos;
using ORS.OrderRobot.ServiceContracts.ThridPartApi;

namespace ORS.OrderRobot.Service.ThridPartApi
{
	public class ThirdProductService : IThirdProductService,ISingleton
    {
        private readonly IHttpHelper _httpHelper;
        private readonly ILogger<ThirdProductService> _logger;
        public ThirdProductService(
            IHttpHelper httpHelper,
            ILogger<ThirdProductService> logger
        )
		{
            _httpHelper = httpHelper;
            _logger = logger;
		}

        public async Task<string?> GetProductDetail(string productId)
        {
            var url = $"/products/{productId}";
            try
            {
                var result = await _httpHelper.SendAsync(url, HttpMethod.Get, new RequestInfo { DisableErrLog = true });
                if (result.StatusCode == 200 && !string.IsNullOrEmpty(result.Body)) return result.Body;
                return null;
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// 通过详情页探测商品是否存在
        /// </summary>
        /// <param name="productId"></param>
        /// <returns></returns>
        public async Task<bool> ProductExists(string productId)
        {
            var url = $"/products/{productId}";
            try
            {
                var result = await _httpHelper.SendAsync(url, HttpMethod.Head,new RequestInfo { DisableErrLog = true});
                return result.StatusCode == 200;
            }
            catch
            {
                return false;
            }
        }
    }
}

